﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPLS.Commons.Enums
{
    public  enum DeviceTypeEnum:int
    {
        [Description("全部设备")]
        ALL=0,
        [Description("ALD设备")]
        ALD =1,


    }
    /// <summary>
    /// 获取改设备的描述信息
    /// </summary>
    public static class DeviceTypeEnumExtensions
    {
        /// <summary>
        /// 获取设备类型的描述信息
        /// </summary>
        /// <param name="deviceType"></param>
        /// <returns></returns>
        public static string GetDescription(this DeviceTypeEnum deviceType)
        {
            var type = deviceType.GetType();
            var memberInfo = type.GetMember(deviceType.ToString());
            if (memberInfo.Length > 0)
            {
                var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    return ((DescriptionAttribute)attributes[0]).Description;
                }
            }
            return deviceType.ToString();
        }

        /// <summary>
        /// 获取所有设备类型枚举
        /// </summary>
        /// <returns></returns>
        public static List<DeviceTypeEnum> GetAllDeviceTypes()
        {
            return Enum.GetValues(typeof(DeviceTypeEnum))
                .Cast<DeviceTypeEnum>()
                .ToList();
        }

        /// <summary>
        /// 获取所有设备类型的描述信息列表
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllDeviceTypeDescriptions()
        {
            return Enum.GetValues(typeof(DeviceTypeEnum))
                .Cast<DeviceTypeEnum>()
                .Select(e => e.GetDescription())
                .ToList();
        }
        /// <summary>
        /// 根据描述获取设备类型枚举
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        public static DeviceTypeEnum GetDeviceTypeByDescription( string description)
        {
            return GetAllDeviceTypes()
                .FirstOrDefault(e => e.GetDescription().ToUpper() == description.ToUpper());
        }
    }

}
